home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / gui / x / xfig.lha / src / x11 / w_grid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-26  |  4.2 KB  |  149 lines

  1. /*
  2.  * FIG : Facility for Interactive Generation of figures
  3.  * Copyright (c) 1985 by Supoj Sutanthavibul
  4.  *
  5.  * "Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both the copyright
  8.  * notice and this permission notice appear in supporting documentation. 
  9.  * No representations are made about the suitability of this software for 
  10.  * any purpose.  It is provided "as is" without express or implied warranty."
  11.  */
  12.  
  13. #include "fig.h"
  14. #include "figx.h"
  15. #include "resources.h"
  16. #include "mode.h"
  17. #include "paintop.h"
  18. #include "object.h"
  19. #include "w_setup.h"
  20. #include "w_util.h"
  21. #include "w_zoom.h"
  22.  
  23. #define null_width 32
  24. #define null_height 32
  25.  
  26. #define MMTOPIX 3
  27. #define IN16TOPIX 5
  28.  
  29. static char    null_bits[null_width * null_height / 8] = {0};
  30.  
  31. static Pixmap    null_pm, grid_pm = 0;
  32. static unsigned long bg, fg;
  33.  
  34. init_grid()
  35. {
  36.     DeclareArgs(2);
  37.  
  38.     if (null_pm == 0) {
  39.     FirstArg(XtNbackground, &bg);
  40.     NextArg(XtNforeground, &fg);
  41.     GetValues(canvas_sw);
  42.  
  43.     null_pm = XCreatePixmapFromBitmapData(tool_d, canvas_win,
  44.                 (char *) null_bits, null_width, null_height,
  45.                       fg, bg, DefaultDepthOfScreen(tool_s));
  46.     }
  47.  
  48.     if (appres.INCHES) {
  49.     posn_rnd[P_MAGNET] = 5; /* 1 x 1/8" */
  50.     posn_hlf[P_MAGNET] = 3;
  51.     posn_rnd[P_GRID1] = 20; /* 4 x 1/8" */
  52.     posn_hlf[P_GRID1] = 10;
  53.     posn_rnd[P_GRID2] = 40; /* 8 x 1/8" */
  54.     posn_hlf[P_GRID2] = 20;
  55.     grid_name[P_MAGNET] = "1/16 in";
  56.     grid_name[P_GRID1] = "1/4 in";
  57.     grid_name[P_GRID2] = "1/2 in";
  58.     grid_fine[P_GRID1] = 1 * IN16TOPIX;    /* 1 x 1/16" */
  59.     grid_coarse[P_GRID1] = 4 * IN16TOPIX;    /* 4 x 1/16" */
  60.     grid_fine[P_GRID2] = 1 * IN16TOPIX;    /* 1 x 1/16" */
  61.     grid_coarse[P_GRID2] = 8 * IN16TOPIX;    /* 8 x 1/16" */
  62.     } else {
  63.     posn_rnd[P_MAGNET] = 3; /* 1 mm */
  64.     posn_hlf[P_MAGNET] = 2;
  65.     posn_rnd[P_GRID1] = 15; /* 5 mm */
  66.     posn_hlf[P_GRID1] = 7;
  67.     posn_rnd[P_GRID2] = 30; /* 10 mm */
  68.     posn_hlf[P_GRID2] = 15;
  69.     grid_name[P_MAGNET] = "1 mm";
  70.     grid_name[P_GRID1] = "5 mm";
  71.     grid_name[P_GRID2] = "10 mm";
  72.     grid_fine[P_GRID1] = 1 * MMTOPIX;    /* 1 mm */
  73.     grid_coarse[P_GRID1] = 5 * MMTOPIX;    /* 5 mm */
  74.     grid_fine[P_GRID2] = 2 * MMTOPIX;    /* 2 mm */
  75.     grid_coarse[P_GRID2] = 10 * MMTOPIX;    /* 10 mm */
  76.     }
  77. }
  78.  
  79. /* grid in X11 is simply the background of the canvas */
  80.  
  81. setup_grid(grid)
  82.     int            grid;
  83. {
  84.     float        coarse, fine;
  85.     float        x, x0c, x0f, y, y0c, y0f;
  86.     int            ic, dim;
  87.     static        prev_grid = -1;
  88.  
  89.     DeclareArgs(1);
  90.  
  91.     if (grid == GRID_0) {
  92.     FirstArg(XtNbackgroundPixmap, null_pm);
  93.     } else {
  94.     if (grid == GRID_1) {
  95.         coarse = grid_coarse[P_GRID1] * zoomscale;
  96.         fine = grid_fine[P_GRID1] * zoomscale;
  97.     } else {
  98.         coarse = grid_coarse[P_GRID2] * zoomscale;
  99.         fine = grid_fine[P_GRID2] * zoomscale;
  100.     }
  101.  
  102.     if (coarse==0.0 && fine==0.0) { /* grid values both zero */
  103.         FirstArg(XtNbackgroundPixmap, null_pm);
  104.     }
  105.     if (coarse == 0.0) {    /* coarse must be <> 0 */
  106.         coarse = fine;
  107.         fine = 0.0;
  108.     }
  109.     ic = (int) coarse;
  110.     dim = (ic > 64.0) ? ic : (64 / ic + 1) * ic;
  111.  
  112.     if (grid_pm)
  113.         XFreePixmap(tool_d, grid_pm);
  114.     grid_pm = XCreatePixmap(tool_d, canvas_win, dim, dim,
  115.                 DefaultDepthOfScreen(tool_s));
  116.     XSetForeground(tool_d, gc, bg);
  117.     XFillRectangle(tool_d, grid_pm, gc, 0, 0, dim, dim);
  118.     XSetForeground(tool_d, gc, fg);
  119.     x0c = round(-round(zoomscale * zoomxoff) % round(coarse) - zoomscale);
  120.     y0c = round(-round(zoomscale * zoomyoff) % round(coarse) - zoomscale);
  121.     if (fine != 0.0) {
  122.         x0f = round(-round(zoomscale * zoomxoff) % round(fine) - zoomscale);
  123.         y0f = round(-round(zoomscale * zoomyoff) % round(fine) - zoomscale);
  124.         for (x = x0c; x < dim; x += coarse)
  125.         for (y = y0f; y < dim; y += fine)
  126.             {
  127.             XDrawPoint(tool_d, grid_pm, gc, round(x), round(y));
  128.             }
  129.         for (y = y0c; y < dim; y += coarse)
  130.         for (x = x0f; x < dim; x += fine)
  131.             {
  132.             XDrawPoint(tool_d, grid_pm, gc, round(x), round(y));
  133.             }
  134.     } else {
  135.         for (x = x0c; x < dim; x += coarse)
  136.         for (y = y0c; y < dim; y += coarse)
  137.             {
  138.             XDrawPoint(tool_d, grid_pm, gc, round(x), round(y));
  139.             }
  140.     }
  141.  
  142.     FirstArg(XtNbackgroundPixmap, grid_pm);
  143.     }
  144.     SetValues(canvas_sw);
  145.     if (prev_grid == GRID_0 && grid == GRID_0)
  146.     redisplay_canvas();
  147.     prev_grid = grid;
  148. }
  149.